package fr.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import fr.business.ManageApplicationsBusinessService;
import fr.form.Application;
import fr.form.synthese.SyntheseEcranApplications;
import fr.utilities.Constantes;

@Controller
@RequestMapping("/superuser")
public class ManageSuApplicationsController {

	@Autowired
	private ManageApplicationsBusinessService bs;

	@RequestMapping("/showApps")
	public String listApplications(Map<String, Object> map,@ModelAttribute(Constantes.APPLICATIONS_SU_FORM)
	SyntheseEcranApplications ecran, BindingResult result, HttpSession session) {
		
		ecran.prepareSUappsEcranStatutRecherche(session.getAttribute("statutApplication"));
		List<Application> apps  = bs.getApplicationsByStatut(ecran.getStatutRecherche());
		map.put("listApplication",apps);
		ecran.prepareSUappsEcran(apps);
		return "superUser/showApps";

	}

	@RequestMapping(value="/valider",method=RequestMethod.POST)
	public String modifier(Map<String, Object> map,@ModelAttribute(Constantes.APPLICATIONS_SU_FORM)
		SyntheseEcranApplications ecran, BindingResult result){
		
		Integer id = ecran.getIdApp();
		
		bs.updateApplication(id,ecran.getMapLien().get(id),ecran.getMapLibelle().get(id),
				ecran.getMapMontant().get(id),ecran.getMapStatuts().get(id),ecran.getMapLogo().get(id));
		
		return Constantes.REDIRECT_SU_SHOW_APPS;
	}

	@RequestMapping(value="/supprimer",method=RequestMethod.POST)
	public String supprimer(Map<String, Object> map,@ModelAttribute(Constantes.APPLICATIONS_SU_FORM)
		SyntheseEcranApplications ecran, BindingResult result){
		
		bs.deleteApplication(ecran.getIdApp());
		return Constantes.REDIRECT_SU_SHOW_APPS;
	}

	@RequestMapping(value="/ajouter",method=RequestMethod.POST)
	public String ajouter(Map<String, Object> map,@ModelAttribute(Constantes.APPLICATIONS_SU_FORM)
	SyntheseEcranApplications ecran, BindingResult result){
		bs.createApplication(ecran.getLogoNew(),ecran.getLienNew(),
				ecran.getLibelleNew(),ecran.getMontantNew(),ecran.getStatutNew());
		
		return Constantes.REDIRECT_SU_SHOW_APPS;

	}

	@RequestMapping(value="/validerTout",method=RequestMethod.POST)
	public String validerModification(Map<String, Object> map,@ModelAttribute(Constantes.APPLICATIONS_SU_FORM)
	SyntheseEcranApplications ecran, BindingResult result) {
		bs.updateStatutsApplications(ecran);
		return Constantes.REDIRECT_SU_SHOW_APPS;
	}

	@RequestMapping(value="/rechercherParStatut",method=RequestMethod.POST)
	public String rechercherParStatut(Map<String, Object> map,@ModelAttribute(Constantes.APPLICATIONS_SU_FORM)
		SyntheseEcranApplications ecran,HttpSession session) {
		session.setAttribute("statutApplication",ecran.getStatutRecherche());
		return Constantes.REDIRECT_SU_SHOW_APPS;
	}



}
